<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class Claim extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_claim';



    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-07-12 16:10
     */
    public function getCateIdAttr($value,$data){

        if(!empty($data['id'])){

            $dis = [

                'b.status'   => 1,

                'a.goods_id' => $data['id'],

                'a.type'     => 3,

                'b.type'     => 2,
            ];

            $goods_cate_model = new GoodsCate();

            $list = $goods_cate_model->alias('a')
                ->join('lbfarm_land_cate b','a.cate_id = b.id')
                ->where($dis)
                ->field('a.*,b.title')
                ->group('a.id')
                ->order('a.id dsec')
                ->column('b.id');


            return array_values($list);
        }

    }

    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-01-26 15:16
     */
    public function getTextAttr($value,$data){

        if(!empty($value)){

            return @unserialize($value);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2022-01-07 10:50
     * @功能说明:轮播图转格式
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-06 15:24
     * @功能说明:周期
     */
    public function getCycleAttr($value,$data){

        if(isset($data['start_time'])&&isset($data['end_time'])){

            $day = ceil(($data['end_time']-$data['start_time'])/86400);

            return $day;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $g_data = $data;

        if(isset($data['process'])){

            unset($data['process']);
        }

        if(isset($data['monitor'])){

            unset($data['monitor']);
        }

        if(isset($data['cate_id'])){

            unset($data['cate_id']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(!empty($data['text'])){

            $data['text'] = serialize($data['text']);
        }

        $data['create_time'] = time();

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        $this->updateSome($id,$g_data);

        return $id;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $g_data = $data;

        if(isset($data['process'])){

            unset($data['process']);
        }

        if(isset($data['monitor'])){

            unset($data['monitor']);
        }

        if(isset($data['cate_id'])){

            unset($data['cate_id']);
        }

        if(!empty($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(!empty($data['text'])){

            $data['text'] = serialize($data['text']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($g_data['process'])||isset($g_data['monitor'])){

            $this->updateSome($dis['id'],$g_data);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-14 15:45
     * @功能说明:添加关联
     */
    public function updateSome($id,$data){

        $server = new \app\farm\server\Claim();

        $claim_text_model = new ClaimText();

        $monitor_text_model= new MonitorText();

        $goods_cate_model = new GoodsCate();

        $server->addObserver($goods_cate_model);

        $server->addObserver($claim_text_model);

        $server->addObserver($monitor_text_model);

        $server->notify($id,$data);

        return true;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function indexDataList($dis,$alh,$sort=1){

        switch ($sort){

            case 1:
                $order = 'id desc';

                break;

            case 2:

                $order = 'sale_num desc,id desc';

                break;

            case 3:

                $order = 'distance asc,id desc';

                break;

        }

        $data = $this->where($dis)->field(['*',$alh])->order($order)->paginate(10)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-12-20 14:00
     * @功能说明:前端土地列表
     */
    public function indexDataListV2($dis,$alh,$sort=1){

        switch ($sort){

            case 1:
                $order = 'a.id desc';

                break;

            case 2:

                $order = 'a.sale_num desc,a.id desc';

                break;

            case 3:

                $order = 'a.distance asc,a.id desc';

                break;

        }

        $data = $this->alias('a')
            ->join('lbfarm_v2_goods_cate b','a.id = b.goods_id','left')
            ->where($dis)
            ->field(['a.*',$alh])
            ->group('a.id')
            ->order($order)
            ->paginate(10)
            ->toArray();

        return $data;


    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }







}